<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Ionize, creative CMS
 *
 * @package		Ionize
 * @author		Ionize Dev Team
 * @license		http://ionizecms.com/doc-license
 * @link		http://ionizecms.com
 * @since		Version 0.9.0
 */

// ------------------------------------------------------------------------

/**
 * Ionize, creative CMS Article Controller
 *
 * @package		Ionize
 * @subpackage	Controllers
 * @category	Article
 * @author		Ionize Dev Team
 *
 */
 
class pr_excel extends MY_admin 
{

	/**
	 * Fields on wich the htmlspecialchars function will not be used before saving
	 * 
	 * @var array
	 */
	/**
	 * Fields on wich no XSS filtering is done
	 * 
	 * @var array
	 */
	/**
	 * Constructor
	 *
	 */
	public function __construct()
	{
		parent::__construct();
		// product model
		$this->load->model('pr_excel_model', '', true);
		//$this->load->model('sibilling_model', '', true);
	}

	// ------------------------------------------------------------------------
	/**
	 * Default : Do nothing
	 *
	 */
	function index()
	{
		//point to pr_product view, then pr_product point to pr_product_list
		$this->output('pr_product');
	}
	//function to export to excel
	function toExcelAll() {
		$this->template['data1'] = $this->pr_excel_model->ToExcelAll();
		//trace($query['data1']);
		$this->output('pr_excel_view');
	}
	
	/**
	 * Export the users list
	 *
	 */
	function export($format = NULL)
	{
		//$format = ( ! is_null($format)) ? $format : $this->input->post('format');
	
		// Load download helper
		$this->load->helper('download');
		
		//$metas = ( ! empty($_POST['metas']) ) ? $_POST['metas'] : array();
		
		// Get users & users meta data
		//$users = $this->users_model->get_list($metas);
		
		$users = $this->pr_excel_model->ToExcelAll();
		//trace($query['data1']);
		//$this->output('pr_excel_view');
		
		// If users, get the format
		if (!empty($users))
		{
			// Export in in asked format
			switch($format)
			{
				case 'csv': $this->_export_csv($users);
			}
			
			$this->success(lang('ionize_message_users_exported'));

		}
		else
		{
			$this->error(lang('ionize_message_users_not_exported'));		
		}

	}
	/**
	 * Export the email list to CSV
	 *
	 */
	private function _export_csv($users)
	{
		$data= array();

		// Add columns names to file header
		$data[] = implode(';', array_keys($users[0]));

		// Add users to data table
		foreach($users as $user)
		{
			$data[] = implode(';', $user);
		}
		
		// Add new line 
		$data = implode("\r\n", $data);

		// File name
		$name = Settings::get('theme').'_users.csv';

		// Send the file to the user
		force_download($name, $data);
	}

}


/* End of file article.php */
/* Location: ./application/controllers/admin/article.php */